﻿using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace MultiConvert
{
    public sealed class MSWordWrapper : MultiConvertApplicationWrapper
    {

        internal new IMswApplication Application
        {
            get
            {
                return (IMswApplication)base.Application;
            }
            set
            {
                base.Application = value;
            }
        }
        internal new MSWDocumentConverter Document
        {
            get
            {
                return (MSWDocumentConverter)base.Document;
            }
            set
            {
                base.Document = value;
            }
        }
        internal IMswDocuments Documents;

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public MSWordWrapper()
        {
            try
            {
                this.Application = (IMswApplication)Marshal.GetActiveObject("Word.Application");
                this.Documents = (IMswDocuments)this.Application.Documents;
            }
            catch (System.Runtime.InteropServices.COMException COMex)
            {
                System.Diagnostics.Trace.WriteLine(COMex.ToString());
                this.Application = (IMswApplication)(new MultiConvert.MswApplicationClass());
                this.Documents = (IMswDocuments)this.Application.Documents;
            }
        }

        public override void OpenDocument(String fromFile)
        {
            IMswDocument document = this.Application.Documents.Open(fromFile);
            this.Document = new MSWDocumentConverter(document, this);
            this.Document.RefreshFields();
        }

        public override void CloseDocuments()
        {
            this.Documents.Close(true);
        }

        public override String ConvertDocument(String convertMethod, String toFileName)
        {
            return this.Document.convert(convertMethod, toFileName);
        }

        public override void ArrangeApplication()
        {
            //Nothing to do
        }

        public override void ReArrangeApplication()
        {
            //Nothing to do
        }
    }
}
